<?php
    $lang = 'en';
    $NewsRow = Null;
    $newsID = Null;
    $add  = "index.php?r=NewsManage/InsertNews";
    $edit = "index.php?r=NewsManage/EditNews";
    $mode = empty($_REQUEST['mode'])?"Add":$_REQUEST['mode'];
    if( $mode == "Edit") {
        $newsID = $_REQUEST['NewsID'];
        $actions = $edit;
        $NewsRow = NewsManage::getNewsDetail($newsID);
    }else{
        $actions = $add;
    }
    
    $curDate = date("Y-m-d");
    
    $titleEN    = $mode=="Edit"?$NewsRow['TitleEN']: "";
    $titleTH    = $mode=="Edit"?$NewsRow['TitleTH']:"";
    $infoEN     = $mode=="Edit"?$NewsRow['InfoEN'] :"";
    $infoTH     = $mode=="Edit"?$NewsRow['InfoTH'] :"";
    $createdate = $mode=="Edit"?$NewsRow['CreateDate']:$curDate;
    $editdate   = $curDate;
    $status     = $mode=="Edit"?$NewsRow['Status'] :"P";
?>
<div>
    <form action="<?=$actions?>" method="post">
        <ul class="nav nav-tabs" id="tabZone">
            <li class="active"><a href="#EN" data-toggle="tab">EN</a></li>
            <li><a href="#TH" data-toggle="tab">TH</a></li>
            <li><a href="#Prop" data-toggle="tab">Property</a></li>
        </ul>
        <div class="tab-content">
            <div class ="tab-pane active" id="EN">
                <div class="hero-unit"><h3>Article news in English language</h3>
                    <div><h4>Title</h4><input id="titleEN" name="titleEN"  type="text" class="span10" placeholder="newstitle EN" value="<?=$titleEN?>"/></div>
                    <div><h4>Information</h4></div>
                    <div><textarea id='infoEN' name="infoEN" class="span10"><?=$infoEN?></textarea></div>
                </div>
            </div>
            <div class ="tab-pane" id="TH">
                <div class="hero-unit"><h3>Article news in thai language</h3>
                    <div><h4>Title</h4><input id="titleTH" name="titleTH" type="text" class="span10" placeholder="newstitle TH" value="<?=$titleTH?>"/></div>
                    <div><h4>Information</h4></div>
                    <div><textarea id='infoTH' name="infoTH" class="span10"><?=$infoTH?></textarea></div>
                </div>
            </div>
            <div class ="tab-pane" id="Prop">
                <div class="hero-unit">
                    <h4 title="Setting news property ">News Property</h4>
                    <h5><b>Create date  </b></h5><input type="text" value="<?=$lang == 'en' ? GM::setformDateEN($createdate) : GM::setformDateTH($createdate)?>" disabled=true >
                    <p><h5><b>State</b></h5>
                        <p>
                            <select id="status" name="status" style="width: 9em" mode="<?=$mode?>" status="<?=$status?>">
                                <option value="P">Public</option>
                                <option value="U">Unpublic</option>
                            </select>
                        </p>
                    </p>
                    <input type="hidden" id="newsid" name="newsid" value="<?=$newsID?>"/>
                    <input type="hidden" id="createdate" name="createdate" value="<?=$createdate?>"/>
                    <input type="hidden" id="editdate" name="editdate" value="<?=$editdate?>"/>
                </div>
            </div>
        </div>
        <div class="divRight">
            <input type="submit" class="btn btn-large" data-loading-text="Loading..." value="Save">
        </div>
    </form>
</div>

<script>
    
        $(document).ready(function(){
            var status = $('select#status').attr('status');
            var str = "#status > option[value=" + status + "]";
            $(str).attr('selected',true);
                
            $('textarea').ckeditor({
                language: 'en',
                extraPlugins: 'image',
                filebrowserBrowseUrl:       'js/ckeditor/filemanager/browser/default/browser.html?Connector='+encodeURIComponent('../../connectors/php/connector.php'),  
                filebrowserImageBrowseUrl : 'js/ckeditor/filemanager/browser/default/browser.html?Type=Image&Connector='+encodeURIComponent('../../connectors/php/connector.php'),  
                filebrowserUploadUrl :      'js/ckeditor/filemanager/connectors/php/upload.php',  
                filebrowserImageUploadUrl : 'js/ckeditor/filemanager/connectors/php/upload.php?Type=Image',  
                on: {
                    instanceReady: function() {
                        $('.cke_button__image').click(genJsonImgList);
                        $('body').on('click',"a:contains('Browse Server')",genJsonImgList);
                    }
                }
            });
        });
        
        function genJsonImgList(){
            $.post('genjsonimglist.php', {}, function(){}, 'text');
        }
</script>